#include "search_city.h"

Search_city::Search_city(QWidget *parent) : QWidget(parent)
{ 
    initUI();
}

// 实现事件过滤器  
// 参数：obj-事件源对象；event-发生的事件
bool Search_city::eventFilter(QObject *obj, QEvent *event) {
    // 判断事件源是否为搜索按钮，且事件类型为鼠标左键点击
    if (obj == m_searchBtn && event->type() == QEvent::MouseButtonPress) {  
        QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);  // 转换为鼠标事件
        if (mouseEvent->button() == Qt::LeftButton) {  // 确认是左键点击
            onSearch(); // 触发搜索逻辑  
            return true;  // 表示事件已处理，不再传递
        }  
    }  
    // 其他事件交给父类处理
    return QWidget::eventFilter(obj, event);  
} 

void Search_city::initUI()
{
    // ---- 初始化控件 ----
        QFont lineEdit_font;
        lineEdit_font.setFamily("楷体");
        lineEdit_font.setPointSize(12);
        m_searchEdit = new QLineEdit(this);
        m_searchEdit->setFont(lineEdit_font);
        m_searchEdit->setPlaceholderText("请输入地区");
        m_searchEdit->move(8,8);
        m_searchEdit->setFixedSize(250,35);
    
        m_searchBtn = new QLabel(this);
        m_searchBtn->setPixmap(QPixmap(":/img/weather/search1.png"));
        m_searchBtn->setFixedSize(25, 25);
        m_searchBtn->setScaledContents(true);
        m_searchBtn->installEventFilter(this); 
        
        
        m_locationLabel = new QLabel("湖南省.长沙市", this);
        QFont font;
        font.setPointSize(18);
        font.setBold(true);
        font.setFamily("楷体");
        m_locationLabel->setFont(font);
        
        // ---- 布局搭建 ----
        QHBoxLayout *editLayout  = new QHBoxLayout(m_searchEdit);
        editLayout->addStretch();
        editLayout ->addWidget(m_searchBtn);
        // 添加搜索按钮，设置对齐方式为居中
        editLayout->addWidget(m_searchBtn, 0, Qt::AlignVCenter);
    
        QHBoxLayout *mainLayout = new QHBoxLayout(this);
        mainLayout->addLayout(editLayout);
        mainLayout->addStretch(); // 添加伸缩项，将标签推到右侧
        mainLayout->addWidget(m_locationLabel, 0, Qt::AlignRight); // 右对齐
    
        setLayout(mainLayout);
        setStyleSheet("background-color: #e9f5ff; border-radius: 8px;");
    
    }
    
CityInfo Search_city::searchCity(const QString &cityName)
    {
        CityInfo result;
        QFile file(":/file/city.txt");
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
            QMessageBox::critical(this, "错误", "无法打开 city.txt 文件！");
            return result;
        }
    
        QTextStream in(&file);
        QString line;
        while (in.readLineInto(&line)) {
            // 按制表符分割 ID 和城市名（格式：101250101  长沙市）
            QStringList parts = line.split("\t");
            if (parts.size() == 2) {
                QString id = parts[0].trimmed();
                QString name = parts[1].trimmed();
                // 模糊匹配（不区分大小写）
                if (name.contains(cityName, Qt::CaseInsensitive)) {
                    result.id = id;
                    result.name = name;
                    break;
                }
            }
        }
        file.close();
    
        if (result.name.isEmpty()) {
            QMessageBox::warning(this, "未找到", "未在 city.txt 中找到该城市！");
        }
        return result;
    }
    
void Search_city::onSearch()
    {
        QString input = m_searchEdit->text().trimmed();
        if (input.isEmpty()) {
            QMessageBox::information(this, "提示", "请输入城市名称！");
            return;
        }
    
        CityInfo city = searchCity(input);
        if (!city.name.isEmpty()) {
            m_locationLabel->setText(city.name);
            m_currentCityId = city.id;
            qDebug() << "当前城市 ID：" << m_currentCityId;
            
            // 发射信号：传递城市ID给主窗口
            emit cityIdChanged(m_currentCityId);
            
            
            m_searchEdit->clear();  // 清空输入框
        }
    }

    
